package com.ants.boot.wx.service.impl;

import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.wx.dao.WxAddressMapper;
import com.ants.boot.wx.domain.WxAddress;
import com.ants.boot.wx.dto.wxaddress.AddAddressDto;
import com.ants.boot.wx.dto.wxaddress.EditAddressDto;
import com.ants.boot.wx.dto.wxaddress.QueryWxAddressDto;
import com.ants.boot.wx.service.WxAddressService;
import com.ants.boot.wx.vo.wxbanner.AddWxBannerDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/1 14:40
 */
@Service
public class WxAddressServiceImpl extends ServiceImpl<WxAddressMapper, WxAddress> implements WxAddressService {
    @Override
    public Page<WxAddress> pageInfo(QueryWxAddressDto dto) {
        QueryWrapper<WxAddress> queryWrapper = new QueryWrapper<>();
        Page<WxAddress> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        if (StringUtils.isNotBlank(dto.getAreaCode())){
            queryWrapper.lambda().eq(WxAddress::getAreaCode,dto.getAreaCode());
        }
        if (StringUtils.isNotBlank(dto.getUserId())){
            queryWrapper.and(wxAddressQueryWrapper -> wxAddressQueryWrapper.lambda().eq(WxAddress::getUserId,dto.getUserId()));
        }
        return this.page(page,queryWrapper);
    }

    @Override
    public List<WxAddress> listByUserId(Integer userId) {
        QueryWrapper<WxAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(WxAddress::getUserId,userId);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean addAddress(AddAddressDto dto) {
        Integer userId = SecurityUtils.getLoginUser().getUserId();
        WxAddress wxAddress = new WxAddress();
        wxAddress.setUserId(userId);
        BeanUtils.copyProperties(dto,wxAddress);
        if (wxAddress.getDefaultAddress().equals("1")){
            updateAllDefault(userId);
        }
        return this.save(wxAddress);
    }

    @Override
    @Transactional
    public boolean editAddress(EditAddressDto dto) {
        Integer userId = SecurityUtils.getLoginUser().getUserId();
        WxAddress wxAddress = new WxAddress();
        BeanUtils.copyProperties(dto,wxAddress);
        wxAddress.setUserId(userId);
        if (wxAddress.getDefaultAddress().equals("1")){
            updateAllDefault(userId);
        }
        return saveOrUpdate(wxAddress);
    }

    @Override
    @Transactional
    public boolean delAddress(List<Integer> ids) {
        if (ids.isEmpty()){
            return false;
        }
        return this.removeByIds(ids);
    }

    private void updateAllDefault(Integer userId){
        UpdateWrapper<WxAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(WxAddress::getDefaultAddress,"0")
                .eq(WxAddress::getUserId,userId);
        this.update(updateWrapper);
    }

}
